import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Student extends JFrame {

	PreparedStatement ps = null;
	Connection ct = null;
	ResultSet rs = null;
	JTable table = null;
	public static int Type = 0;

	public Student(String title) {

		Container conn = this.getContentPane();
		conn.setLayout(new GridLayout(2, 1));
		// 得到窗口的容器
		Vector<String> columnNames = new Vector<String>();
		columnNames.add("学号");
		columnNames.add("姓名");
		columnNames.add("性别");
		columnNames.add("出生日期");
		Vector rowData = new Vector();
		// rowData行数据，可以存放多行

		Vector studentData = new Vector();
		try {
			// 加载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			// 得到连接
			ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC",
					"root", "123456");

			ps = ct.prepareStatement("select * from tblstudent");

			rs = ps.executeQuery();

			while (rs.next()) {
				// rowData可以存放多行
				// Vector hang=new Vector();
				studentData = new Vector();
				studentData.add(rs.getString(1));
				studentData.add(rs.getString(2));
				studentData.add(rs.getString(3));
				studentData.add(rs.getString(4));

				// 加入到rowData
				rowData.add(studentData);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (ct != null) {
					ct.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		table = new JTable(rowData, columnNames);
		JScrollPane scrollpane = new JScrollPane(table);
		table.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {

				int row = table.getSelectedRow();
				int col = table.getSelectedColumn();
				String studID = table.getValueAt(row, 0).toString();
				String stuName = table.getValueAt(row, 1).toString();
				String stuSex = table.getValueAt(row, 2).toString();
				String birth = table.getValueAt(row, 3).toString();
				if (e.getKeyChar() == KeyEvent.VK_ENTER) {
					if (Type == 1) {
						// add操作
						try {
							Class.forName("com.mysql.cj.jdbc.Driver");
							Connection conn = (Connection) DriverManager.getConnection(
									"jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC", "root",
									"123456");
							PreparedStatement ps = conn.prepareStatement("insert into tblstudent values('" + studID
									+ "','" + stuName + "','" + stuSex + "','" + birth + "')");
							ps.executeUpdate();
						} catch (Exception w) {
							JOptionPane.showMessageDialog(scrollpane, "Connection Error!");
						}
					} else if (Type == 2) {
						// edit操作
						try {
							Class.forName("com.mysql.cj.jdbc.Driver");
							Connection conn = (Connection) DriverManager.getConnection(
									"jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC", "root",
									"123456");
							PreparedStatement ps = conn.prepareStatement("update tblstudent set stuName='" + stuName +"',stuSex='" + stuSex  + "',birth='" + birth  + "' where studID='" + studID + "'");
							ps.executeUpdate();
						} catch (Exception w) {
							JOptionPane.showMessageDialog(scrollpane, "Connection Error!");
						}
					}
				}
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// JLabel L1 = new JLabel("Hello,world!"); //创建一个标签 并设置初始内容

		conn.add(scrollpane);
		JPanel dbPanel = new JPanel();
		JButton add = new JButton("增加");
		add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Type = 1;
				DefaultTableModel model = (DefaultTableModel) table.getModel();
				model.addRow(new Vector());
			}

		});
		JButton delete = new JButton("删除");
		delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				if (row != -1) {
					DefaultTableModel model = (DefaultTableModel) table.getModel();

					String selected = model.getValueAt(row, 0).toString();

					model.removeRow(row);

					try {
						Class.forName("com.mysql.cj.jdbc.Driver");
						Connection conn = (Connection) DriverManager.getConnection(
								"jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC", "root",
								"123456");
						PreparedStatement ps = conn
								.prepareStatement("delete from tblstudent where studID=" + selected + " ");
						ps.executeUpdate();
					} catch (Exception w) {
						JOptionPane.showMessageDialog(scrollpane, "Connection Error!");
					}

				} else {
					JOptionPane.showMessageDialog(scrollpane, "请选择一行数据!");
				}

			}
		});
		JButton edit = new JButton("修改");

		edit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Type = 2;

			}

		});
		dbPanel.add(add);
		dbPanel.add(delete);
		dbPanel.add(edit);

		conn.add(dbPanel);
		this.setTitle(title);
		this.setBounds(200, 200, 400, 300); // 设置窗口的属性 窗口位置以及窗口的大小
		this.setVisible(true);// 设置窗口可见
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Student("学生信息管理");
	}

}
